package demo4;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WHY
 * Date: 2022-11-16
 * Time: 14:14
 */
class Student  implements Comparable<Student>{
    public String name;
    public int age;
    public int score;

    public Student(String name, int age, int score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }
    public int compareTo(Student o){//根据年龄比较排序
        if(this.age>o.age){
            return 1;
        }else if(this.age<o.age){
            return -1;
        }else{
            return 0;
        }
    }
}
//根据姓名字母排序
 /* if(this.name.compareTo(o.name) > 0) {
            return 1;
        }else if(this.name.compareTo(o.name) < 0) {
            return -1;
        }else {
            return 0;
        }*/
//使用冒泡排序实现依据年龄排序
public class Test {
    public static void sort(Comparable[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                /*if(array[j] > array[j+1]) {
                    交换；
                }*/
                if(array[j].compareTo(array[j+1]) > 0) {
                    Comparable tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }
    public static void main(String[] args) {
        Student[]  students=new Student[3];
        students[0]=new Student("wangyibo",25,100);
        students[1]=new Student("weihongyan",19,99);
        students[2]=new Student("gaoyan",19,95);
        System.out.println(students[0].compareTo(students[1]));
        /*sort(students);
        System.out.println(Arrays.toString(students));*/
    }

}
